home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / debug / common / symify.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-21  |  3.0 KB  |  136 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <pc.h>
  6. #include <debug/syms.h>
  7. #include <debug/tss.h>
  8.  
  9. #define SC(r,c) (*(char *)(sc + (r)*ScreenCols() + (c)))
  10. #define SW(r,c) (*(sc + (r)*ScreenCols() + (c)))
  11.  
  12. TSS a_tss;
  13. int main(int argc, char **argv)
  14. {
  15.   int r, c;
  16.   short *sc;
  17.   char buf[90];
  18.   int i, lineno;
  19.   unsigned v;
  20.   unsigned long d;
  21.   char *func, *file;
  22.   FILE *ofile=0;
  23.   FILE *ifile=0;
  24.  
  25.   if (argc < 2)
  26.   {
  27.     fprintf(stderr, "Usage: symify [-o <outfile>] [-i <corefile>] <program>\n");
  28.     fprintf(stderr, "This program adds debug information to DJGPP program call frame tracebacks\n");
  29.     return 1;
  30.   }
  31.   while (argv[1][0] == '-')
  32.   {
  33.     if ((strcmp(argv[1], "-o") == 0) && (argc > 3))
  34.     {
  35.       ofile = fopen(argv[2], "w");
  36.       if (ofile == 0)
  37.         fprintf(stderr, "Error: unable to open file %s\n", argv[2]);
  38.       argc -= 2;
  39.       argv += 2;
  40.     }
  41.     if ((strcmp(argv[1], "-i") == 0) && (argc > 3))
  42.     {
  43.       ifile = fopen(argv[2], "r");
  44.       if (ifile == 0)
  45.         fprintf(stderr, "Error: unable to open file %s\n", argv[2]);
  46.       argc -= 2;
  47.       argv += 2;
  48.     }
  49.   }
  50.   syms_init(argv[1]);
  51.  
  52.   if (ifile)
  53.   {
  54.     char line[1000];
  55.     if (ofile == 0)
  56.       ofile = stdout;
  57.     while (fgets(line, 1000, ifile))
  58.     {
  59.       if (strncmp(line, "  0x", 4) == 0)
  60.       {
  61.         sscanf(line+4, "%x", &v);
  62.         func = syms_val2name(v, &d);
  63.         file = syms_val2line(v, &lineno, 0);
  64.         fprintf(ofile, "  0x%08x", v);
  65.         if (func)
  66.         {
  67.           fprintf(ofile, " %s", func);
  68.           if (d)
  69.             fprintf(ofile, "%+ld", d);
  70.         }
  71.         if (file)
  72.         {
  73.           if (func)
  74.             fprintf(ofile, ", ");
  75.           fprintf(ofile, "line %d of %s", lineno, file);
  76.         }
  77.         fputc('\n', ofile);
  78.       }
  79.       else
  80.         fputs(line, ofile);
  81.     }
  82.     return 0;
  83.   }
  84.  
  85.   sc = (short *)malloc(ScreenRows() * ScreenCols() * 2);
  86.  
  87.   ScreenRetrieve(sc);
  88.  
  89.   for (r=0; r<ScreenRows(); r++)
  90.   {
  91.     if (SC(r,0) == ' ' && SC(r,1) == ' ' && SC(r,2) == '0' && SC(r,3) == 'x')
  92.     {
  93.       buf[8] = 0;
  94.       for (i=0; i<8; i++)
  95.         buf[i] = SC(r, i+4);
  96.       sscanf(buf, "%x", &v);
  97.       func = syms_val2name(v, &d);
  98.       file = syms_val2line(v, &lineno, 0);
  99.       buf[0] = 0;
  100.       if (func)
  101.       {
  102.     strcpy(buf, func);
  103.     if (d)
  104.       sprintf(buf+strlen(buf), "%+ld", d);
  105.       }
  106.       if (file)
  107.       {
  108.         if (buf[0])
  109.           strcat(buf, ", ");
  110.         sprintf(buf+strlen(buf), "line %d of %s", lineno, file);
  111.       }
  112.       if (buf[0])
  113.         for (i=0; buf[i]; i++)
  114.           SW(r, 15+i) = 0x0f00 + buf[i];
  115.     }
  116.   }
  117.  
  118.   if (ofile)
  119.   {
  120.     for (r=0; r<ScreenRows(); r++)
  121.     {
  122.       c = 0;
  123.       for (i=0; i<ScreenCols(); i++)
  124.         if (SC(r, i) != ' ')
  125.           c = i;
  126.       for (i=0; i<=c; i++)
  127.         fputc(SC(r,i), ofile);
  128.       fputc('\n', ofile);
  129.     }
  130.     fclose(ofile);
  131.   }
  132.   else
  133.     ScreenUpdate(sc);
  134.   return 0;
  135. }
  136.